﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AesonBaseFramework.Annotations;
using AesonBaseFramework.EntityFramework.DbContexts;

namespace AesonBaseFramework.EntityFramework.DataManagers
{
    public abstract class AesonBaseSqlDataManager<TContext, TManager> : IDisposable
        where TContext : AesonSqlDbContext
    {
        public static TContext Ctx;

        protected AesonBaseSqlDataManager(TContext t)
        {
                
        }

        #region Create instance of TManager
        // Create a instance of the calling Datamanager passing in constructor the adequat
        //COntext based on the parametres. (This is an Abstract Class)
        public static TManager Create()
        {
            return (TManager)Activator.CreateInstance(typeof(TManager), CreateContext());
        }
        public static TManager Create(DbConnectionInfo info)
        {
            Ctx = CreateContext(info);
            return (TManager)Activator.CreateInstance(typeof(TManager), Ctx);
        }
     
        #endregion

        #region Create instabce of TContext
        private static TContext CreateContext()
        {

            return Activator.CreateInstance<TContext>();
        }

        private static TContext CreateContext(DbConnectionInfo info)
        {
            return (TContext)Activator.CreateInstance(typeof(TContext),info);
        }
      
        #endregion

        #region Dispose

        public void Dispose()
        {
            if (Ctx == null) return;
            Ctx.Dispose();
            Ctx = null;
        }
        #endregion
    }

}
